package mac10.monitor;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import mac10.models.WorldModel;

public class NewMonitor extends JFrame
{

	private static final long serialVersionUID = -9158474539367972358L;
	public static final int WIDTH = 600;
	public static final int HEIGHT = 620;
	
	private WorldComponent gridWorld;
    private int totalSteps;
    private JLabel scoreLabel, cowsLabel, stepLabel;
    private JCheckBox drawGridCheckBox, drawSimpleCheckBox, drawEdgesCheckBox;
    
    public NewMonitor(WorldModel worldModel)
    {
        super("Monitor - team name:"+worldModel.getTeamName());
        this.totalSteps = worldModel.getTotalSteps();
        this.gridWorld = new WorldComponent(worldModel);
        this.setLayout(new BorderLayout());
        this.initNorth();
        this.add(gridWorld, BorderLayout.CENTER);
        
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int width = (WIDTH > d.width)? d.width : WIDTH;
        int height = (HEIGHT > d.height)? d.height : HEIGHT;
        Dimension min = gridWorld.getMinimumSize();
        width = (width > min.width)? width : min.width;
        height = (height > min.height+20)? height : min.height+20;
        this.setMinimumSize(new Dimension(min.width, min.height+20));
        this.setSize(width, height);
        int p = (d.width - width) / 2;
        int q = (d.height - height) / 2;
        this.setLocation(p, q);
        
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private void initNorth ()
    {
        JPanel north = new JPanel();
        
        JLabel label1 = new JLabel("Score: ");
        north.add(label1);
        this.scoreLabel = new JLabel("0");
        north.add(scoreLabel);
        JLabel label2 = new JLabel("  Cows in corral: ");
        north.add(label2);
        this.cowsLabel = new JLabel("0");
        north.add(cowsLabel);
        JLabel label3 = new JLabel("  Step: ");
        north.add(label3);
        this.stepLabel = new JLabel("0");
        north.add(stepLabel);
        JLabel label4 = new JLabel("/"+totalSteps);
        north.add(label4);
        
        drawGridCheckBox = new JCheckBox("draw grid", CellComponent.drawGrid);
        drawGridCheckBox.addChangeListener(new ChangeListener()
        {
            public void stateChanged (ChangeEvent e)
            {
                CellComponent.drawGrid = drawGridCheckBox.isSelected();
            }
        });
        north.add(drawGridCheckBox);
        
        drawSimpleCheckBox = new JCheckBox("simple", CellComponent.drawSimple);
        drawSimpleCheckBox.addChangeListener(new ChangeListener()
        {
            public void stateChanged (ChangeEvent e)
            {
                CellComponent.drawSimple = drawSimpleCheckBox.isSelected();
            }
        });
        north.add(drawSimpleCheckBox);
        
        drawEdgesCheckBox = new JCheckBox("Edges", true);
        drawEdgesCheckBox.addChangeListener(new ChangeListener() {
			
			public void stateChanged(ChangeEvent e) {
				CellComponent.drawEdgeDetection = drawEdgesCheckBox.isSelected();
			}
		});
        north.add(drawEdgesCheckBox);
        
        this.add(north, BorderLayout.NORTH);
    }
    
    public void updateMonitor(WorldModel worldModel)
    {
        scoreLabel.setText(""+worldModel.getScore());
        cowsLabel.setText(""+worldModel.getCowsInCorral());
        int currStep = worldModel.getCurrStep();
        stepLabel.setText(""+currStep);
        gridWorld.updateWorld(worldModel);
//        this.repaint();
    }
}
